<?php

namespace Home\Controller;

use Think\Controller;

class HomeController extends Controller
{
    public function home()
    {
        import('ORG.Util.Page');// 导入分页类
        $username = M('patient')->select();
        $items = M('items')->select();
        //总数
        $list_counts = M('operation')
                    ->join("items on items.id=operation.iid")
                    ->join("patient on patient.id=operation.pid")
                    ->order('time desc')
                    ->count();

        $Page = new \Think\Page($list_counts,10);// 实例化分页类 传入总记录数
        $list = M('operation')
                    ->join("items on items.id=operation.iid")
                    ->join("patient on patient.id=operation.pid")
                    ->limit($Page->firstRow.','.$Page->listRows)
                    ->order('time desc')
                    ->select();
        
        $Page->setConfig('header','条记录');
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('first','第一页');
        $Page->setConfig('last','最后一页');
        $Page->setConfig('%totalRow%','aaa');
        $show = $Page->show();// 分页显示输出
//         dump($list);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('users',$username);
        $this->assign('items',$items);
        $this->assign('list',$list);
        $this->show();
    }

    public function items()
    {
        $items = M('items')->order('id')->select();
        $this->assign('items', $items);
        $this->show();
    }

    public function member()
    {
//        dump(ACTION_NAME);
        $patient = M('patient');
        $data = $patient->order('id desc')->select();
        $this->assign('list', $data);
        $this->display();
    }

    //增加人员
    public function add()
    {
        $dalldata = I('post.');
        $data = Array(
            'name' => $dalldata['username'],
            'sex' => $dalldata['sex'],
            'age' => $dalldata['age'],
            'phone' => $dalldata['phoneNumber'],
            'comeTime' => $dalldata['comeTime']
        );
        M('patient')->add($data);
        $this->redirect('Home/home/member');
//       $this->success('添加成功',U('Home/member'));
    }

    //删除人员
    public function delete()
    {
        $deleteId = I('get.deleteId');
        M('patient')->where('id = ' . $deleteId)->delete();
        M('operation')->where('pid='.$deleteId)->delete();//删除与该人员相关的操作
        $this->redirect('Home/home/member');
    }

    //搜索人员
    public function search()
    {
        $searchContent = I('get.searchContent');
        $where['name'] = array('like', '%' . $searchContent . '%');
        $data = M('patient')->where($where)->select();
        $this->assign('list', $data);
        $this->display('Home/member');
    }

    //增加项目
    public function addItems()
    {
        $itemName = I('post.itemName');
        $itemPrice = I('post.itemPrice');
        $data = Array(
            'xname' => $itemName,
            'xprice' => $itemPrice
        );
        M('items')->add($data);
        $this->redirect('Home/home/items');
    }

    //删除项目
    public function deleteItem()
    {
        $deleteId = I('get.deleteId');
        $where['id'] = $deleteId;
        M('items')->where($where)->delete();
        $this->redirect('Home/home/items');
    }

    //搜索项目
    public function searchItem()
    {
        $searchContent = I('get.searchContent');
        $where['xname'] = array('like', '%' . $searchContent . '%');
        $data = M('items')->where($where)->select();
        $this->assign('items', $data);
        $this->display('Home/items');
    }

    //增加一个操作
    public function addOperation()
    {
        $opera = I('post.');
        $itemArray = $opera['data']['itemArray'];
        $pid = $opera['data']['pid'];
        $time = $opera['data']['time'];
        foreach ($itemArray as $val){
            $data = Array(
                'pid' => $pid,
                'iid' => $val['itemid'],
                'cishu' => $val['cishu'],
                'time' => $time
            );
            M('operation')->add($data);
        }
        $this->ajaxReturn(1);
    }

    //删除操作项
    public function deleteOpera()
    {
        $deleteId = I('get.deleteId');
        $where['operaid'] = $deleteId;
        M('operation')->where($where)->delete();
        $this->redirect('Home/home');
    }

    //搜索一个操作
    public function searchOpera()
    {
        $searchContent = I('get.searchContent');
        $where['xname'] = array('like', '%' . $searchContent . '%');
        $data = M('items')->where($where)->select();
        $this->assign('items', $data);
        $this->display('Home/items');
    }

    //人员详情
    public function memberDetails()
    {
        $memberId = I('get.memberId');
        $where['pid']=$memberId;
        $list = M('operation')
                    ->where($where)
                    ->join("items on items.id=operation.iid")
                    ->join("patient on patient.id=operation.pid")
                    ->order('time desc')
                    ->select();
        // dump($memberId);
        $this->assign('list',$list);
        $this->show();
    }
}





















